package com.anran.passenger.service.impl;

import com.anran.common.module.dto.NumberCodeDTO;
import com.anran.common.response.ResponseResult;
import com.anran.passenger.remote.OnlineServiceVerificationCodeClient;
import com.anran.passenger.service.VerificationCodeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @Author: anran
 * @Date: 2022 - 09 - 28
 */
@Service
public class VerificationCodeServiceImpl implements VerificationCodeService {

    @Resource
    private OnlineServiceVerificationCodeClient onlineServiceVerificationCodeClient;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    final static Logger logger = LoggerFactory.getLogger(VerificationCodeServiceImpl.class);

    //乘客验证码前缀
    private String verificationCodePrefix = "passenger-verification-code-";

    @Override
    public ResponseResult generationCode(String passengerPhone) {

        //调用获取验证码服务
        ResponseResult<NumberCodeDTO> responseResult = onlineServiceVerificationCodeClient.numberCode(4);
        //验证码
        int code = responseResult.getData().getNumberCode();

        logger.info("获取验证码：{}", code);
        //存入redis

        //构建key
        String key = verificationCodePrefix + passengerPhone;

        stringRedisTemplate.opsForValue().set(key, String.valueOf(code), 2, TimeUnit.MINUTES);

        //根据不同运营商 发送短信

        return ResponseResult.success();
    }
}
